#include "datahandler.h"


#include "./tools/data/contentdealer.h"

DataHandler DataHandler::m_instance;
DataHandler& DataHandler::getInstance()
{
    return m_instance;
}


DataHandler::DataHandler(QObject *parent)
    : QObject{parent}
{

}

DataHandler::~DataHandler()
{

}

void DataHandler::handleSuddenLogout(const qintptr descriptor, const QString UserId)
{

}

void DataHandler::handleByteData(const qintptr descriptor, const QByteArray originData)
{
    qDebug() << "开始处理数据";
    QByteArray unpackedData;
    ContentType ct = ContentDealer::Unpack(originData, unpackedData);
    switch (ct) {
    case ContentType::MESSAGE: {
        handleMessage(descriptor, unpackedData);
        break;
    }
    case ContentType::IMAGE: {
        handleImage(descriptor, unpackedData);
        break;
    }
    case ContentType::OTHER: {

    }
    }
}

void DataHandler::sendLoginResponse(const qintptr descriptor, const LoginResponseMessage loginResponseMessage)
{
    QByteArray responseByteArray = ContentDealer::JsonPack(loginResponseMessage);
    emit dataReadyToSend(descriptor, responseByteArray);
}

void DataHandler::sendRegisterResponse(const qintptr descriptor, const RegisterResponseMessage registerResponseMessage)
{
    QByteArray responseByteArray = ContentDealer::JsonPack(registerResponseMessage);
    emit dataReadyToSend(descriptor, responseByteArray);
}

void DataHandler::sendLogoutResponse(const qintptr descriptor, const LogoutResponseMessage logoutResponseMessage)
{
    QByteArray responseByteArray = ContentDealer::JsonPack(logoutResponseMessage);
    emit dataReadyToSend(descriptor, responseByteArray);
}

void DataHandler::sendSearchUserResponse(const qintptr descriptor, const SearchUserResponseMessage searchUserResponseMessage)
{
    QByteArray responseByteArray = ContentDealer::JsonPack(searchUserResponseMessage);
    emit dataReadyToSend(descriptor, responseByteArray);
}

void DataHandler::sendFriendApplicationRequest(const QString userId, const ApplyFriendRequestMessageToClient afrmtc)
{
    QByteArray responseByteArray = ContentDealer::JsonPack(afrmtc);
    emit dataReadyToSend(userId, responseByteArray);
}

void DataHandler::sendFriendApplicationResultResponse(const QString userId, const ApplyFriendResponseMessageToClient afrmtc)
{
    QByteArray responseByteArray = ContentDealer::JsonPack(afrmtc);
    emit dataReadyToSend(userId, responseByteArray);
}

// 准备转发给某个用户下线期间，其他用户发的聊天消息
void DataHandler::sendNewChatMsg(const QString receiverId, const ChatMessageToClient cmtc)
{
    QByteArray responseByteArray = ContentDealer::JsonPack(cmtc);
    emit dataReadyToSend(receiverId, responseByteArray);
}

// 准备转发视频通话请求结果给请求客户端
void DataHandler::sendVideoResponseToRequestUser(const QString requestUserId, const VideoResponseToClient vrtc)
{
    QByteArray responseByteArray = ContentDealer::JsonPack(vrtc);
    emit dataReadyToSend(requestUserId, responseByteArray);
}

// 准备转发视频通话请求给另一个客户端
void DataHandler::sendVideoRequestToResponseUser(const QString responseUserId, const VideoRequestToClient vrtc)
{
    QByteArray responseByteArray = ContentDealer::JsonPack(vrtc);
    emit dataReadyToSend(responseUserId, responseByteArray);
}



void DataHandler::handleMessage(const qintptr descriptor, const QByteArray& messageByteArray)
{
    qDebug() << "处理消息";
    QJsonObject jsonObj;
    MessageType mt = ContentDealer::GetMessageType(messageByteArray, jsonObj);
    // qDebug() << static_cast<int>(mt);

    QByteArray responseByteArray;
    switch (mt) {
    case MessageType::LoginRequest: {   // 登录请求
        LoginRequestMessage loginRequestMessage(jsonObj);
        // qDebug() << "收到登录请求";
        emit dealLoginRequestMessage(descriptor, loginRequestMessage);
        break;
    }
    case MessageType::RegisterRequest: {    // 注册请求
        RegisterRequestMessage registerRequestMessage(jsonObj);
        emit dealRegisterRequestMessage(descriptor, registerRequestMessage);
        break;
    }
    case MessageType::LogoutRequest: {  // 登出请求
        LogoutRequestMessage logoutRequestMessage(jsonObj);
        emit dealLogoutRequestMessage(descriptor, logoutRequestMessage);
        break;
    }
    case MessageType::SearchUserRequest: {  // 搜索用户请求
        SearchUserRequestMessage searchUserRequestMessage(jsonObj);
        emit dealSearchUserRequestMessage(descriptor, searchUserRequestMessage);
        break;
    }
    case MessageType::ApplyFriendRequestToServer: { // 来自客户端的好友申请
        ApplyFriendRequestMessageToServer afrmts(jsonObj);
        emit dealApplyFriendRequestMessageToServer(descriptor, afrmts);
        break;
    }
    case MessageType::ApplyFriendResponseToServer: {    // 来自客户端的好友申请回应
        ApplyFriendResponseMessageToServer afrm(jsonObj);
        emit dealApplyFriendResponseMessage(descriptor, afrm);
        break;
    }
    case MessageType::RequestDataBuffer: {  // 来自客户端的消息缓存请求
        RequestBufferDataMessage rbdm(jsonObj);
        emit dealRequestBufferDataMessage(descriptor, rbdm);
        break;
    }
    case MessageType::GetFriendApplication: {   // 客户端收到好友申请转发
        qDebug() << "收到客户端关于好友申请的确认消息";
        GetApplicationFromServer gafs(jsonObj);
        emit dealFriendApplicationSendStatus(descriptor, gafs);
        break;
    }
    case MessageType::GetFriendApplicationResult: { // 客户端收到好友审核结果
        qDebug() << "收到用户审核完毕其他人好友申请";
        GetApplicationResultFromServer garfs(jsonObj);
        emit dealFriendApplicationResultSendStatus(descriptor, garfs);
        break;
    }
    case MessageType::ChatMessageToServer: {    // 客户端发送消息给其好友
        qDebug() << "收到客户端给好友发聊天消息";
        ChatMessageToServer cmts(jsonObj);
        emit dealChatMessage(descriptor, cmts);
        break;
    }
    case MessageType::GetChatMessageFromServer: {   // 客户端发送确认消息表示收到了其他用户聊天消息
        qDebug() << "收到客户端关于聊天信息的确认消息";
        GetChatMessageFromServer gcmfs(jsonObj);
        emit dealChatMsgStatus(descriptor, gcmfs);
        break;
    }
    case MessageType::VideoRequestToServer: {   // 客户端发起的视频通话请求
        qDebug() << "收到客户端发起的视频通话请求";
        VideoRequestToServer vrts(jsonObj);
        emit dealVideoRequest(descriptor, vrts);
        break;
    }
    case MessageType::VideoResponseToServer: {  // 客户端回复的视频通话请求结果
        qDebug() << "客户端回复的视频通话请求结果";
        VideoResponseToServer vrts(jsonObj);
        emit dealVideoResponse(descriptor, vrts);
        break;
    }
    default:
        break;
    }
}

void DataHandler::handleImage(const qintptr descriptor, const QByteArray& imageByteArray)
{

}
